package cn.edu.neusoft.food729.model;

import java.util.List;

import cn.edu.neusoft.food729.Bean.CommentsBean;
import cn.edu.neusoft.food729.common.Common;
import cn.edu.neusoft.food729.iface.Commentsiface;
import cn.edu.neusoft.food729.listener.CommentsListener;
import cn.edu.neusoft.food729.service.CommentsService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class CommentsModel implements Commentsiface {
    private Retrofit retrofit;

    public CommentsModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(Common.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getResultList(int food_id, final CommentsListener listener) {
        CommentsService service = retrofit.create(CommentsService.class);
        Call<List<CommentsBean>> call = service.getComments(food_id);

        call.enqueue(new Callback<List<CommentsBean>>() {
            @Override
            public void onResponse(Call<List<CommentsBean>> call, Response<List<CommentsBean>> response) {
                if (response.isSuccessful() && response.body() != null)
                    listener.onResponse(response.body());
                else listener.onFail("on response fail");
            }

            @Override
            public void onFailure(Call<List<CommentsBean>> call, Throwable t) {
                listener.onFail(t.toString());
                System.out.println("--DemoModel--" + t.toString());
            }
        });
    }
}

